package com.yic.module.base.controller.admin.workshop;

import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.enums.CommonStatusEnum;
import com.yic.module.base.api.center.dto.CenterRespDTO;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import com.yic.module.base.api.item.dto.ItemRespDTO;
import com.yic.module.base.controller.admin.center.vo.CenterSimpleRespVO;
import com.yic.module.base.convert.center.CenterConvert;
import com.yic.module.base.dal.dataobject.center.CenterDO;
import com.yic.module.base.dal.dataobject.factory.FactoryDO;
import com.yic.module.base.service.factory.FactoryService;
import com.yic.module.system.api.dept.dto.DeptRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.base.controller.admin.workshop.vo.*;
import com.yic.module.base.dal.dataobject.workshop.WorkshopDO;
import com.yic.module.base.convert.workshop.WorkshopConvert;
import com.yic.module.base.service.workshop.WorkshopService;

@Tag(name = "管理后台 - 生产车间")
@RestController
@RequestMapping("/base/workshop")
@Validated
public class WorkshopController {

    @Resource
    private WorkshopService workshopService;
    @Resource
    private FactoryService factoryService;

    @PostMapping("/create")
    @Operation(summary = "创建生产车间")
    @PreAuthorize("@ss.hasPermission('base:workshop:create')")
    public CommonResult<Long> createWorkshop(@Valid @RequestBody WorkshopCreateReqVO createReqVO) {
        return success(workshopService.createWorkshop(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产车间")
    @PreAuthorize("@ss.hasPermission('base:workshop:update')")
    public CommonResult<Boolean> updateWorkshop(@Valid @RequestBody WorkshopUpdateReqVO updateReqVO) {
        workshopService.updateWorkshop(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产车间")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:workshop:delete')")
    public CommonResult<Boolean> deleteWorkshop(@RequestParam("id") Long id) {
        workshopService.deleteWorkshop(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产车间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:workshop:query')")
    public CommonResult<WorkshopRespVO> getWorkshop(@RequestParam("id") Long id) {
        WorkshopDO workshop = workshopService.getWorkshop(id);
        return success(WorkshopConvert.INSTANCE.convert(workshop));
    }

    @GetMapping("/list")
    @Operation(summary = "获得生产车间列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('base:workshop:query')")
    public CommonResult<List<WorkshopRespVO>> getWorkshopList(@RequestParam("ids") Collection<Long> ids) {
        List<WorkshopDO> list = workshopService.getWorkshopList(ids);
        return success(WorkshopConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产车间分页")
    @PreAuthorize("@ss.hasPermission('base:workshop:query')")
    public CommonResult<PageResult<WorkshopRespVO>> getWorkshopPage(@Valid WorkshopPageReqVO pageVO) {
        PageResult<WorkshopDO> pageResult = workshopService.getWorkshopPage(pageVO);
        PageResult<WorkshopRespVO> resp = WorkshopConvert.INSTANCE.convertPage(pageResult);
        resp.getList().forEach(workshop -> {
            //封装工厂信息
            FactoryDO factory = factoryService.getFactory(workshop.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                workshop.setFactoryName(factory.getName());
            }
        });
        return success(resp);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产车间 Excel")
    @PreAuthorize("@ss.hasPermission('base:workshop:export')")
    @OperateLog(type = EXPORT)
    public void exportWorkshopExcel(@Valid WorkshopExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WorkshopDO> list = workshopService.getWorkshopList(exportReqVO);
        // 导出 Excel
        List<WorkshopExcelVO> datas = WorkshopConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "生产车间.xls", "数据", WorkshopExcelVO.class, datas);
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取生产车间精简信息列表", description = "只包含被开启的生产车间，主要用于前端的下拉选项")
    public CommonResult<List<WorkshopSimpleRespVO>> getSimpleWorkshopList() {
        // 获得工作中心列表，只要开启状态的
        List<WorkshopDO> list = workshopService.getWorkshopList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(WorkshopDO::getSort));
        return success(WorkshopConvert.INSTANCE.convertList03(list));
    }

}
